package com.cuckoo.mall.thus.menuaccount.favorite;


import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bgn.baseframe.base.BaseListFragment;
import com.bgn.baseframe.utils.BaseStringUtil;
import com.bgn.baseframe.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cuckoo.mall.R;
import com.cuckoo.mall.javabean.CartProductItem;
import com.cuckoo.mall.javabean.Product;
import com.cuckoo.mall.thus.menucart.CartQiuckAdapter;
import com.cuckoo.mall.thus.utils.DialogHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.bgn.baseframe.base.BaseActivity.ID;

/**
 * 作者：wl on 2018/4/ * 邮箱：wanglun@stosz.com
 */

public class FavoriteFragment extends BaseListFragment<FavoriteContract.View, FavoritePresenter, Product> implements FavoriteContract.View {

    @BindView(R.id.check_state)
    CheckBox checkState;
    @BindView(R.id.tv_del)
    TextView tvDel;
    @BindView(R.id.lly_del)
    LinearLayout llyDel;


    @Override
    protected boolean shoudOpenLoadMore() {
        return false;
    }

    @Override
    protected boolean shoudSetEmptyView() {
        return false;
    }

    @Override
    protected boolean shoudOpenRefresh() {
        return true;
    }

    @Override
    protected BaseQuickAdapter setAdapter() {

        adapter = new FavoriteListAdapter(datas, new CartQiuckAdapter.ItemCheckChange() {
            @Override
            public void whenItemCheckChangeCllBack() {
                resetBootmViewData();
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ARouter.getInstance().build("/mall/goodMultipleDetailActivity").withString(ID, datas.get(position).getProduct_id()).navigation();
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, final int position) {

                switch (view.getId()) {
                    case R.id.tv_add_cart:

                        break;
                }

            }
        });
        return adapter;
    }

    @Override
    protected void setLayoutManage() {
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        recycleView.setLayoutManager(manager);
    }


    @Override
    protected void onRefreshData() {
        mPresenter.loadAllFavorites(HTTP_REFRESH_LIST);
    }

    @Override
    protected void loadMoreData() {

    }

    @Override
    protected void initListData() {
        showLoadingView();
        mPresenter.loadAllFavorites(HTTP_INIT_LIST);
    }

    @Override
    public void whenGetFavorites(List<Product> temDatas, int type) {
        if (temDatas == null || temDatas.size() == 0) {
            datas.clear();
            adapter.notifyDataSetChanged();
            showNoDataDefaultView(R.mipmap.default_bg_nodata, R.string.default_no_data);
            ((FavoriteActivity) getActivity()).togleEditButton(false);
        } else {
            ((FavoriteActivity) getActivity()).togleEditButton(true);
            handleData(temDatas, type);
        }
    }


    @Override
    protected int getLaoutResouse() {
        return R.layout.fragment_save_list;
    }

    @Override
    protected void initOtherUi(View rootView) {
        ButterKnife.bind(this, rootView);
    }

    @OnClick({R.id.check_state, R.id.tv_del})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.check_state:
                if (datas != null && datas.size() > 0) {
                    for (Product item : datas) {
                        item.setSelected(checkState.isChecked());
                        adapter.notifyDataSetChanged();
                    }
                }
                break;
            case R.id.tv_del:
                if (isNetworkerConnectHint()) {
                    final String ids = getCartIdStrings();
                    if (TextUtils.isEmpty(ids)) {
                        ToastUtil.showInfo(getString(R.string.select_delete_product));
                        return;
                    }

                    DialogHelper.showConfirmDialog(getActivity(), getString(R.string.sure_to_cancel_favorite), getString(R.string.confirm), getString(R.string.cancel), new DialogHelper.DialogPressCallBack() {
                        @Override
                        public void whenPressRightButton() {
                            mPresenter.deleteFavorite(ids, getSelectedPositions());
                        }
                    });

//                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//                    builder.setTitle(R.string.are_you_sure);
//                    builder.setMessage(R.string.delete_tip);
//                    builder.setPositiveButton(R.string.delete_yes, new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            showLoadingView();
//                            mPresenter.deleteFavorite(ids, getSelectedPositions());
//                        }
//                    });
//                    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            dialog.dismiss();
//                        }
//                    });
//                    builder.create().show();


                }
                break;
        }
    }

    /*******************获取选择的位置数组*********************/
    private List<Integer> getSelectedPositions() {
        List<Integer> selects = new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            if (datas.get(i).isSelected()) {
                selects.add(i);
            }
        }
        return selects;
    }

    /*******************获取选择的位置对应的ID数组*********************/
    private String getCartIdStrings() {
        String idsString = "";
        for (int i = 0; i < datas.size(); i++) {
            if (datas.get(i).isSelected()) {
                idsString = idsString + datas.get(i).getProduct_id() + ",";
            }
        }
        if (!TextUtils.isEmpty(idsString)) {
            idsString = idsString.substring(0, idsString.length() - 1);
        }
        return idsString;
    }

    public void setMode(boolean editMode) {
        ((FavoriteListAdapter) adapter).setEditMode(editMode);
        if (editMode) {
            llyDel.setVisibility(View.VISIBLE);
        } else {
            llyDel.setVisibility(View.GONE);
        }
    }

    @Override
    public void whenDeleteSucceed(List<Integer> positions) {
        ToastUtil.showInfo(getString(R.string.delete_succeed));
//        if (datas.size() > 0) {
//            adapter.remove(position);
//            adapter.notifyDataSetChanged();
//        } else {
//            datas.clear();
//            adapter.notifyDataSetChanged();
//            showNoDataDefaultView("No Data");
//        }

        for (int i = positions.size() - 1; i >= 0; i--) {
            adapter.remove(positions.get(i));
        }

        if (datas == null || datas.size() == 0) {
            showNoDataDefaultView(R.mipmap.default_bg_nodata, R.string.default_no_data);
            ((FavoriteActivity) getActivity()).togleEditButton(false);
        }

        resetBootmViewData();
    }

    /*********重置底部View的数据******/
    private void resetBootmViewData() {
        boolean isAllChecked = true;
        for (Product item : datas) {
            if (!item.isSelected()) {
                isAllChecked = false;
            }
        }
        if (datas != null && datas.size() > 0) {
            checkState.setChecked(isAllChecked);
        } else {
            checkState.setChecked(false);
        }
    }

//    @Override
//    protected void setEmptyView() {
//        if (shoudSetEmptyView()) {
//            adapter.setEmptyView(R.layout.default_no_data);
//        }
//    }
}
